package com.zhanbo.zhanbowymusic.main.featured;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.ArtistAlbumBean;
import com.mwx.baselib.bean.PageSongListBean;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentSingerAlbumPlaylistBinding;
import com.zhanbo.zhanbowymusic.main.featured.adapter.SingerAlbumPlayListAdapter;
import com.zhanbo.zhanbowymusic.music.MusicController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 专辑歌曲列表
 */
public class SingerAlbumPlayListFragment extends BaseFragment<FragmentSingerAlbumPlaylistBinding> {
    private SingerAlbumPlayListAdapter adapter;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_singer_album_playlist;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initData() {
        if (getArguments()==null){
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("albumId",getArguments().getInt("id")+"");
        NetworkManager.getInstance().getApiService().getSongList(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<PageSongListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<PageSongListBean> listBaseResponses) {
                        adapter.getItems().addAll(listBaseResponses.getData().getItems());
                        adapter.notifyDataSetChanged();

                    }
                });
    }

    @Override
    protected void initView() {
        binding.recyclerSingleSong.setLayoutManager(new LinearLayoutManager(_mActivity,LinearLayoutManager.VERTICAL, false));
         adapter = new SingerAlbumPlayListAdapter(_mActivity);
        binding.recyclerSingleSong.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        adapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<SongBean>() {
            @Override
            public void onItemClick(int position) {
                MusicController.musicController.updateItemDate(adapter.getItems().get(position));
            }
        });
    }

    public static SingerAlbumPlayListFragment newInstance(int id) {

        Bundle args = new Bundle();
        args.putInt("id",id);
        SingerAlbumPlayListFragment fragment = new SingerAlbumPlayListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void playAll() {
        if (adapter!=null&&adapter.getItems()!=null){
            MusicController.getInstance().updateAndPlaySheetData(adapter.getItems());
        }
    }
}
